//
//  ViewController.m
//  09-UI
//
//  Created by Snow on 16/7/26.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "ViewController.h"
#import "CityViewController.h"
#define kReuse @"reuse"
@interface ViewController ()
@property (nonatomic, strong) NSMutableArray *arrayOfPro;
@property (nonatomic, strong) NSMutableArray *arrayOfCity;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"省份";
    self.navigationController.navigationBar.translucent = NO;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kReuse];
    self.arrayOfPro = [NSMutableArray array];
    self.arrayOfCity = [NSMutableArray array];
    NSString *path = @"/Users/Snow/DLS160710/UI/HomeWork/09-UI/09-UI/area.txt";
    NSString *strSource = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *mainArray = [strSource componentsSeparatedByString:@"\n"];
    NSMutableArray *array = [NSMutableArray array];
    for (NSString *string in mainArray) {
        if (![string hasPrefix:@" "])
        {
            NSMutableDictionary *dicOfProvince = [NSMutableDictionary dictionary];
            [dicOfProvince setValue:string forKey:@"provinceName"];
            [array addObject:dicOfProvince];
            NSMutableArray *arrOfCity = [NSMutableArray array];
            [dicOfProvince setValue:arrOfCity forKey:@"arrayOfCity"];
        }
        if ([string hasPrefix:@"  "] && ![string hasPrefix:@"    "]) {
            NSMutableDictionary *dicOfProvince = [array lastObject];
            NSMutableArray *arrayOfCity = [dicOfProvince objectForKey:@"arrayOfCity"];
            NSMutableDictionary *dicOfCity = [NSMutableDictionary dictionary];
            [dicOfCity setValue:string forKey:@"cityName"];
            [arrayOfCity addObject:dicOfCity];
            NSMutableArray *arrOfArea = [NSMutableArray array];
            [dicOfCity setValue:arrOfArea forKey:@"arrayOfArea"];
        }
        if ([string hasPrefix:@"    "]) {
            NSMutableDictionary *dicOfProvince = [array lastObject];
            NSMutableArray *arrayOfCity = [dicOfProvince objectForKey:@"arrayOfCity"];
            NSMutableDictionary *dicOfCity = [arrayOfCity lastObject];
            NSMutableArray *arrayOfArea = [dicOfCity objectForKey:@"arrayOfArea"];
            [arrayOfArea addObject:string];
        }
    }
    NSLog(@"%@", array);
    for (NSDictionary *dic in array) {
        [self.arrayOfPro addObject:[dic objectForKey:@"provinceName"]];
        [self.arrayOfCity addObject:[dic objectForKey:@"arrayOfCity"]];
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrayOfPro.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kReuse];
    cell.textLabel.text = [_arrayOfPro objectAtIndex:indexPath.row];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CityViewController *city = [[CityViewController alloc] init];
    city.arrOfCity = [self.arrayOfCity objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:city animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
